package io.mya.config;

import io.mya.modules.sys.oauth2.OAuth2Filter;
import io.mya.modules.sys.oauth2.OAuth2Realm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro配置
 *
 * @author PGmm
 * @email yangmeifm@gmail.com
 * @date 2017/09/12 23:00:00
 */
@Configuration
public class ShiroConfig {

    @Bean("sessionManager")
    public SessionManager sessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionValidationSchedulerEnabled(true);
        //sessionManager.setSessionIdUrlRewritingEnabled(false);
        //sessionManager.setSessionIdCookieEnabled(false);
        return sessionManager;
    }

    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Realm oAuth2Realm, SessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuth2Realm);
        securityManager.setSessionManager(sessionManager);

        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        //oauth过滤
        Map<String, Filter> filters = new HashMap<>();
        filters.put("oauth2", new OAuth2Filter());
        shiroFilter.setFilters(filters);



        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/candybox/**", "anon");
        filterMap.put("/candyboxbis/**", "anon");

        filterMap.put("/endpointWeb/**", "anon");
        filterMap.put("/sys/test/**", "anon");

        filterMap.put("/webjars/**", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/api/**", "anon");



        filterMap.put("/sys/login", "anon");
        filterMap.put("/**/*.css", "anon");
        filterMap.put("/**/*.js", "anon");
        filterMap.put("/**/*.html", "anon");
        filterMap.put("/fonts/**", "anon");
        filterMap.put("/plugins/**", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/favicon.ico", "anon");
        filterMap.put("/captcha.jpg", "anon");
        filterMap.put("/img/*.png", "anon");
        filterMap.put("/img/**", "anon");
        filterMap.put("/**/*.gif", "anon");
        filterMap.put("/**/*.png", "anon");
        filterMap.put("/**/*.jpg", "anon");
        filterMap.put("/", "anon");
        filterMap.put("/sys/sysCapitalstatement/exportExcel", "anon");
        filterMap.put("/pay", "anon");
        filterMap.put("/pay/noticequickpay/*", "anon");
        filterMap.put("/pay/b2bb2cpay/*", "anon");
        filterMap.put("/pay/singletransferpay/*", "anon");
        //测试api汇率
        filterMap.put("/sys/exchangeRate/thenearfuture", "anon");
        filterMap.put("/sys/exchangeRate/coinlis", "anon");
        filterMap.put("/sys/exchangeRate/rate", "anon");
        
        filterMap.put("/sys/sysCapitalstatement/exportExcelAl", "anon");
        filterMap.put("/sys/sysCapitalstatement/exportExcelselesct", "anon");
        filterMap.put("/sys/sysCapitalstatement/exportExcelchargelist", "anon");
        filterMap.put("/sys/sysCapitalstatement/exportExcelselelist", "anon");
        filterMap.put("/sys/sysCapitalstatement/countOrderExcelAl", "anon");
        filterMap.put("/sys/sysCapitalstatement/rechargetrad", "anon");
        filterMap.put("/sys/sysCapitalstatement/suggestrad", "anon");
        filterMap.put("/secretkey/key/exportExcel", "anon");
        filterMap.put("/sys/sysProduct/list", "anon");
        filterMap.put("/admin", "anon");
        filterMap.put("/sys/settings/getTitle", "anon");
        filterMap.put("/app/**", "anon");
        filterMap.put("/**", "oauth2");

        
       

        filterMap.put("/sys/test/**", "anon");





        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
